package com.ww.scm.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ww.core.orm.Page;
import com.ww.core.orm.PropertyFilter;
import com.ww.scm.constants.Constants;
import com.ww.scm.entity.pub.Dictionary;
import com.ww.scm.service.DictionaryManager;
import com.ww.scm.valueObject.croe.ResponseEntity;
import com.ww.scm.valueObject.croe.ResponseInfo;
import com.ww.scm.valueObject.croe.ResponseJson;
import com.ww.scm.valueObject.dic.DictionaryVo;

@Controller
@RequestMapping(value = "/dictionary")
public class DictionaryController {
	
	/*
	 * 日志类
	 */
	private static Logger logger = LoggerFactory.getLogger(DictionaryController.class);

	@Autowired
	private DictionaryManager dictionaryManager;
	
	public void setDictionaryManager(DictionaryManager dictionaryManager) {
		this.dictionaryManager = dictionaryManager;
	}
	
	@RequestMapping(params = "method=list")
	@ResponseBody
	public String list(HttpServletRequest request) {
		String limit=request.getParameter("limit");
		String start=request.getParameter("start");

		Page<Dictionary> page = new Page<Dictionary>(start,limit);
		
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		filters.add(new PropertyFilter(Constants.FILTER_EQS_SYSFLAG, Constants.SYS_FLAG_USEING));
		
		page = dictionaryManager.findDictionaryPage(page, filters);
		List<DictionaryVo> returnList=new ArrayList<DictionaryVo>();
		for (Dictionary dictionary : page.getResult()) {
			DictionaryVo dictionaryVo=new DictionaryVo(dictionary,dictionaryManager);
			returnList.add(dictionaryVo);
		}
		ResponseEntity<DictionaryVo> responseEntity=new ResponseEntity<DictionaryVo>();
		responseEntity.setEntity("dictionary", returnList);
		responseEntity.setMaxRecordSizeValue(page.getTotalItems());
		return responseEntity.toJson();
	}
	
    @RequestMapping(params = "method=delete")
    @ResponseBody
    public String delete(String ids) {
    	if (StringUtils.isBlank(ids)){
    		logger.error("删除方法中传入参数ids为空!");
    		ResponseInfo responseInfo=new ResponseInfo(false,"删除方法中传入参数ids为空!");
    		return responseInfo.toJson();
    	}
    	ResponseInfo responseInfo=new ResponseInfo();
    	//todo:这里需要得到登录用户
    	String operator="1001";
    	try {
    		String[] idArray=ids.split(Constants.SEPARATOR_COLON);
            for(int i = 0; i < idArray.length; i++) {
            	dictionaryManager.deleteDictionary(idArray[i], operator);
            }
		} catch (Exception e) {
			logger.error("删除主键字典出错：",e);
			responseInfo.setResponseContent(e.getMessage());
			responseInfo.setResponseSuccess(false);
		}
		return responseInfo.toJson();
    }
    
    @RequestMapping(params = "method=edit")
    @ResponseBody
    public String edit(String id) {
    	if (StringUtils.isBlank(id)){
    		logger.error("修改方法中传入参数id为空!");
    		ResponseInfo responseInfo=new ResponseInfo(false,"修改方法中传入参数id为空!");
    		return responseInfo.toJson();
    	}
    	Dictionary dictionary = dictionaryManager.findDictionaryById(id);
    	DictionaryVo dictionaryVo=new DictionaryVo(dictionary,dictionaryManager);
		ResponseEntity<DictionaryVo> responseEntity=new ResponseEntity<DictionaryVo>();
		responseEntity.setEntity("dictionaryVo", dictionaryVo);
        return responseEntity.toJson();
    }
    
    @RequestMapping(params = "method=save")
    @ResponseBody
    public String save(String dataset) {
    	ResponseInfo responseInfo=new ResponseInfo();
    	try {
    	ResponseJson<DictionaryVo> responseJson=new ResponseJson<DictionaryVo>(dataset);
    	DictionaryVo dictionaryVo=responseJson.toEntity(DictionaryVo.class, "dictionaryVo");
    	dictionaryManager.saveDictionary(dictionaryVo);
    	} catch (Exception e) {
			logger.error("保存出错：",e);
			responseInfo.setResponseContent(e.getMessage());
			responseInfo.setResponseSuccess(false);
		}
    	return responseInfo.toJson();
    }
    
	@RequestMapping(params = "method=findDictionary")
	@ResponseBody
	public String findDictionary(HttpServletRequest request) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		filters.add(new PropertyFilter(Constants.FILTER_EQS_SYSFLAG, Constants.SYS_FLAG_USEING));
		List<Dictionary> dictionaryList= dictionaryManager.findDictionaryList(filters);
		List<DictionaryVo> dictionaryVoList=new ArrayList<DictionaryVo>();
		for (Dictionary dictionary : dictionaryList) {
			DictionaryVo dictionaryVo=new DictionaryVo(dictionary,dictionaryManager);
			dictionaryVoList.add(dictionaryVo);
		}
		ResponseEntity<DictionaryVo> responseEntity=new ResponseEntity<DictionaryVo>();
		responseEntity.setEntity("dictionary", dictionaryVoList);
		return responseEntity.toJson();
	}
	
	@RequestMapping(params = "method=findDictionaryByTypeId")
	@ResponseBody
	public String findDictionaryByTypeId(String typeId) {
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter(Constants.FILTER_EQS_SYSFLAG, Constants.SYS_FLAG_USEING));
		filters.add(new PropertyFilter(Constants.FILTER_EQS_TYPE_ID,  typeId));
		List<Dictionary> dictionaryList= dictionaryManager.findDictionaryList(filters);
		List<DictionaryVo> dictionaryVoList=new ArrayList<DictionaryVo>();
		for (Dictionary dictionary : dictionaryList) {
			DictionaryVo dictionaryVo=new DictionaryVo(dictionary,dictionaryManager);
			dictionaryVoList.add(dictionaryVo);
		}
		ResponseEntity<DictionaryVo> responseEntity=new ResponseEntity<DictionaryVo>();
		responseEntity.setEntity("dictionary", dictionaryVoList);
		return responseEntity.toJson();
	}
    
	/*
	@RequestMapping(params = "method=findTree")
	@ResponseBody
	public String findTree(HttpServletRequest request) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Dictionary dictionary= dictionaryManager.findDictionary(filters);
		ResponseEntity<Dictionary> responseEntity=new ResponseEntity<Dictionary>();
		responseEntity.setEntity("dictionary", dictionary);
		return responseEntity.toJson();
	}
	*/	
	
	
	
	
}
